package de.vocabulary.server.language.translation;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import de.vocabulary.shared.model.Vocable;

public class TranslationProvider {
	
	private Map<String, Set<String>> transMap;
	
	public TranslationProvider(List<Vocable> vocables) {
		initialize();
		
		for (Vocable vocable : vocables) {
			Set<String> translations;
			if (transMap.containsKey(vocable.getLanguage1Word())) {
				translations = transMap.get(vocable.getLanguage1Word());
			}
			else {
				translations = new HashSet<String>();
			}
			translations.add(vocable.getLanguage2Word());
			transMap.put(vocable.getLanguage1Word(),translations);
		}
	}
	
	private void initialize() {
		transMap = new HashMap<String, Set<String>>();
	}
	
	
	public Set<String> getTranslations(String term) {
		if (transMap.containsKey(term)) {
			return transMap.get(term);
		}
		else {
			return Collections.emptySet();
		}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (String key : transMap.keySet()) {
			sb.append(key);
			sb.append("\t");
			sb.append(StringUtils.join(transMap.get(key), ","));
			sb.append("\n");
		}
		return sb.toString();
	}
}